var {
  http
} = require('../../utils/apiutils');
const qiniu = require("../../utils/qnutils");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    min: 0,
    max: 100,
    content: "",
    telphone: "",
    fileList: [],
    uplist:[],
    upImg:[],
  },
  deleteImg:function(e)
  {
    let index = e.detail.index;
    this.data.fileList.splice(index,1);
    this.setData({
      fileList:this.data.fileList,
    })

  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log("---------", file.path);
    var data = {
      url:file.url}
    this.data.fileList.push(data);
    this.data.uplist.push(data);
    this.setData({
      fileList:this.data.fileList,
    })


    // wx.uploadFile({
    //   url: 'https://example.weixin.qq.com/upload', // 仅为示例，非真实的接口地址
    //   filePath: file.path,
    //   name: 'file',
    //   formData: { user: 'test' },
    //   success(res) {
    //     // 上传完成需要更新 fileList
    //     const { fileList = [] } = this.data;
    //     fileList.push({ ...file, url: res.data });
    //     this.setData({ fileList });
    //   },
    // });
  },

  upDataImage: function (tempFilePaths,index) {
    var that = this;
    // console.log("0000000000",tempFilePaths,"------------",index);
    qiniu.getUploadImgae(tempFilePaths,
      function (res) {
        console.log("--------上传后返回的数据--",res);
        that.data.upImg.push(res.key);
        if(that.data.fileList.length-1 == index)
        {
          that.feedback()
        }
        else
        {
            that.upDataImage(that.data.fileList[index+1].url,index+1);
        }
      },
      function (err) {})
  },
  getUpToken: function () {
    var that = this;
    http.getUpToken({},
      function (res) {
        console.log("----七牛云的授权码---", res);
        that.data.token = res.data;
        qiniu.initQiniu(res.data);
        that.data.upImg = []
        that.upDataImage(that.data.fileList[0].url,0);
        
      },
      function (err) {

      })
  },
  feedback:function(){

    var data = {
      "feedbackBo": {
        "content":this.data.content,
        "telphone":this.data.telphone,
    },
    "list": this.data.upImg,
    }
    var that = this;
    http.feedback({},
      function (res) {
        wx.showModal({
          title: '提交成功',
          content: '您的信息已提交，我们会非常重视您的反馈',
          showCancel:false,
          confirmText:"我知道了",
          success(res){
            wx.navigateBack({
              delta: 1,
        
            })
          }
        })
      },
      function (err) {

      })


    console.log("--------反馈建议提交的信息----",data);
    
  },
  onClickSumbit: function () {
    console.log("-----提交数据----",this.data.content,this.data.telphone)
    if(this.data.content.length>2)
    {
      if(this.data.telphone.length==11)
      {

         if(this.data.fileList.length>0)
         {
             this.getUpToken()
         }
         else
         {

           this.feedback();
         }
      }
      else
      {
        wx.showToast({
          title: '请填写正确手机号方便取得联系',
          icon:"none"
        })
      }

    }
    else
    {
       wx.showToast({
         title: '请填写反馈的问题内容',
         icon:"none"
       })
    }

  },
  bindinputP: function (e) {
    var value = e.detail.value;
    this.data.telphone = value;
  },
  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    callback(file.type === 'image');
  },
  inputs: function (e) {
    var value = e.detail.value;
    this.data.content = value;
    var len = parseInt(value.length);
    if (len > this.data.max) return;
    this.setData({
      currentWordNumber: len
    });
    if (this.data.currentWordNumber == 100) {
      wx.showModal({
        title: '提示',
        content: '您输入的文字已达上限',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("-----提交数据----",this.data.content,this.data.telphone)
    this.setData({
      telphone:getApp().globalData.userInfo.telphone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})